package com.bstek.bdf4.mq.core;
import com.bstek.bdf4.mq.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import com.bstek.bdf2.core.mq.publisher.RabbitMQRequestEntity;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
import java.util.Map;

/**
 * @Title: 消息消费者
 * @Package: com.bstek.bdf4.mq.core
 * @Description:
 * @Author: zxq
 * @Date: 2024/2/6 - 16:51
 */

@Slf4j
@Component
public class RouteDeadLetterCustomer {
    public static final String QUEUE = "queue-zxq";
    public static final String ACK = "ack";
    public static final String EXCHANGE = "route_direct";

    /**
     * 死信队列使用：重试除数达到最大
     */


    @RabbitListener(bindings = {@QueueBinding(value = @Queue(name ="#{rabbitMQProPerties.queue}"),
            exchange = @Exchange(value = "#{rabbitMQProPerties.exchange}"),
            key = "#{rabbitMQProPerties.routingKey}" )})
//    @RabbitListener(bindings = {@QueueBinding(value = @Queue(name =QUEUE),
//            exchange = @Exchange(value = ACK),
//            key =EXCHANGE )})
    public void queue(RabbitMQRequestEntity<User> entity, Channel channel, Message message, @Headers Map<String, Object> headers) throws Exception {
        try {
            log.info("交换机模式【direct】,key【queue】,开始消费：{},Channel:{}, Message:{}, Headers :{}", entity, channel, message, headers);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception e) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
        }
    }

}
